﻿using System;
using Kugar.Core.BaseStruct;
using Kugar.Core.ExtMethod;
using Kugar.Tool.MongoDBHelper;
using Kugar.UI.Web.AdminLTE.Interfaces;
using Kugar.UI.Web.AdminLTE.Models;
using Kugar.UI.Web.AdminLTE.NetCore.AdminManager.MongoDB.Entities;
using MongoDB.Bson;
using MongoDB.Driver.Builders;

namespace Kugar.UI.Web.AdminLTE.NetCore.AdminManager.MongoDB
{
    public class AdminManager:IAdminLoginManager,IAdminManager
    {
        private IMongoDBContext _helper = null;
        private IMongoDBCollectionHelper<base_Admin> _adminCol = null;

        public AdminManager(IMongoDBContext context)
        {
            _helper = context ?? throw new ArgumentNullException(nameof(context));

            _adminCol = _helper.GetCollection<base_Admin>("base_Admin");
        }

        public VM_PagedList<Model_AdminInfo> GetAdminList(string keyword = "", int state = -1, int pageIndex = 1, int pageSize = 20)
        {
            var tmp = _helper.GetCollection<base_Admin>("base_Admin");

            var query = Query.EQ("IsDeleted", false);
            if (keyword.IsNotNullOrEmpty()) query = Query.And(query, MongoQueryEx.Like("UserName", keyword));

            if (state != -1) query = Query.And(query, Query.EQ("State", state));

            return tmp.FindAsWithPaged(query, SortBy.Descending("LastLoginDt"), pageIndex: pageIndex,
                pageSize: pageSize).Cast(x => new Model_AdminInfo()
                {
                    Password = x.Password,
                    UserName = x.UserName,
                    State = x.State,
                    UserID = x._id.ToString()
                });
        }

        public Model_AdminInfo GetAdminByID(string userID)
        {
            var tmp = _helper.GetCollection<base_Admin>("base_Admin");

            if (userID.IsNullOrEmpty()) return null;

            var adminInfo = tmp.FindByIDAs(new ObjectId(userID));

            return new Model_AdminInfo()
            {
                Password = adminInfo.Password,
                UserName = adminInfo.UserName,
                State = adminInfo.State,
                UserID = adminInfo._id.ToString()
            };
        }

        public ResultReturn<string> AddAdmin(string userName, string password, int state)
        {
            var tmp = _helper.GetCollection<base_Admin>("base_Admin");

            if (userName.IsNullOrEmpty()) return new FailResultReturn<string>("用户名称不能为空");

            if (password.IsNullOrEmpty()) return new FailResultReturn<string>("密码不能为空");

            if (state == -1) return new FailResultReturn<string>("状态不能为空");

            var adminInfo = new base_Admin
            {
                _id = ObjectId.GenerateNewId(),
                UserName = userName,
                Password = password,
                State = state
            };
            var ret = tmp.Insert(adminInfo);

            if (ret.IsSuccess)
                return new SuccessResultReturn<string>(ret.ReturnData.ToString());
            else
                return new FailResultReturn<string>("添加管理员失败");
        }


        public ResultReturn UpdateAdmin(string userID, int state)
        {
            var tmp = _helper.GetCollection<base_Admin>("base_Admin");

            if (userID.IsNullOrEmpty()) return new FailResultReturn("用户id不能为空");

            if (state == -1) return new FailResultReturn("状态不能为空");

            var query = Query.EQ("_id", new ObjectId(userID));
            var update = Update.Set("State", state);

            return tmp.Update(query, update);
        }

        public ResultReturn DeleteAdminByID(string userID)
        {
            var tmp = _helper.GetCollection<base_Admin>("base_Admin");

            if (userID.IsNullOrEmpty()) return new FailResultReturn("用户id不能为空");

            var query = Query.EQ("_id", new ObjectId(userID));
            var update = Update.Set("IsDeleted", true);

            return tmp.Update(query, update);

        }

        public ResultReturn ChangePassword(string userID, string oldPassword, string newPassword)
        {
            var tmp = _helper.GetCollection<base_Admin>("base_Admin");

            if (userID.IsNullOrEmpty()) return new FailResultReturn("用户id不能为空");

            var adminInfo = tmp.FindByIDAs(new ObjectId(userID));

            if (oldPassword.IsNullOrEmpty() || oldPassword.Length < 6) return new FailResultReturn("请输入旧密码");

            if (adminInfo.Password != oldPassword) return new FailResultReturn("请输入正确的旧密码");

            if (newPassword.IsNullOrEmpty() || newPassword.Length < 6) return new FailResultReturn("请输入长度大于6位的新密码");

            var query = Query.EQ("_id", new ObjectId(userID));
            var update = Update.Set("Password", newPassword);

            return tmp.Update(query, update);
        }

        public ResultReturn ResetPassword(string userID)
        {
            var tmp = _helper.GetCollection<base_Admin>("base_Admin");

            if (userID.IsNullOrEmpty()) return new FailResultReturn("用户id不能为空");

            var query = Query.EQ("_id", new ObjectId(userID));
            var update = Update.Set("Password", "123456");

            return tmp.Update(query, update);
        }

        public ResultReturn<string> Login(string userName, string password)
        {
            var tmp = _helper.GetCollection<base_Admin>("base_Admin");

            var adminInfo = tmp.FindOneAs(Query.And(Query.EQ("UserName", userName), Query.EQ("Password", password)));

            if (adminInfo != null)
            {
                var login = new SuccessResultReturn<string>(adminInfo._id.ToString());
                return new SuccessResultReturn<string>(adminInfo._id.ToString());
            }
            else
            {
                return new FailResultReturn<string>("用户名或密码错误");
            }

        }
    }
}
